/**
 * Created by waterworld on 17-7-26.
 */
var fs=require('fs');
var P = require("bluebird");
var pfs= P.promisifyAll(fs);

function writeFile(file,str){
    // 测试用的中文
    // appendFile，如果文件不存在，会自动创建新文件
    // 如果用writeFile，那么会删除旧文件，直接写新文件
    fs.appendFile(file, str, function(err){
        if(err)
            console.log("写入文件 failed " + err);
    });
}

var util={
 getDateTime:function()
{
    var t=new Date();
    var year= t.getFullYear();
    var mon= t.getMonth()+1;if(mon<10) mon='0'+mon;
    var date= t.getDate();if(date<10) date='0'+date;
    var hour = t.getHours(); if(hour<10) hour='0'+hour;
    var minute= t.getMinutes(); if(minute<10) minute='0'+minute;
    var second= t.getSeconds(); if(second<10) second='0'+second;
    var  ms= Math.floor((t.getTime()%10000)/10);
    var s=year+'-'+mon+'-'+date+' '+hour+':'+minute+':'+second+'.'+ms;
    return s;
},

 writePer15Minute:function(str)
{
    var t=new Date();
    var year= t.getFullYear();
    var mon= t.getMonth()+1;if(mon<10) mon='0'+mon;
    var date= t.getDate();if(date<10) date='0'+date;
    var hour = t.getHours(); if(hour<10) hour='0'+hour;
    var minute= t.getMinutes(); if(minute<10) minute='0'+minute;
    var second= t.getSeconds(); if(second<10) second='0'+second;
    var  ms= Math.floor((t.getTime()%10000)/10);
    var m=Math.floor(minute/15);
    var file='./../alog/a10dban/'+year+'-'+mon+'-'+date+' '+hour+'-'+m+'.txt';
    writeFile(file,str+'\r\n');
},

writePer15mAsync: P.coroutine(function*(str)
{
    var t=new Date();
    var year= t.getFullYear();
    var mon= t.getMonth()+1;if(mon<10) mon='0'+mon;
    var date= t.getDate();if(date<10) date='0'+date;
    var hour = t.getHours(); if(hour<10) hour='0'+hour;
    var minute= t.getMinutes(); if(minute<10) minute='0'+minute;
    var second= t.getSeconds(); if(second<10) second='0'+second;
    var  ms= Math.floor((t.getTime()%10000)/10);
    var m=Math.floor(minute/15);
    var file='./../alog/d89/'+year+'-'+mon+'-'+date+' '+hour+'-'+m+'.txt';
    str+='\r\n';
    yield  pfs.appendFileAsync(file, str);
}),


writeRoomLogAsync: P.coroutine(function*(roomId,str)
{
    var t=new Date();
    var year= t.getFullYear();
    var mon= t.getMonth()+1;if(mon<10) mon='0'+mon;
    var date= t.getDate();if(date<10) date='0'+date;
    var hour = t.getHours(); if(hour<10) hour='0'+hour;
    var minute= t.getMinutes(); if(minute<10) minute='0'+minute;
    var second= t.getSeconds(); if(second<10) second='0'+second;
    var  ms= Math.floor((t.getTime()%10000)/10);
    var m=Math.floor(minute/15);
    var file='./../alog/d89/'+year+'-'+mon+'-'+date+' '+hour+'-'+m+'_'+roomId+'.txt';
    str+='\r\n';
    yield  pfs.appendFileAsync(file, str);
}),
getToday:function()
{
    var  today=new Date();
    today.setHours(0);today.setMinutes(0);today.setSeconds(0);today.setMilliseconds(0);
    return today;
},
tick2DateStr:function(tick)
{
    var t=new Date();
    t.setTime(tick);
    var y = t.getFullYear();
    var m = t.getMonth()+1;
    m = (m<10 ? "0"+m:m);
    var d = t.getDate();
    d = (d<10 ? "0"+d:d);
    var h = t.getHours();
    h = (h<10 ? "0"+h:h);
    var minu = t.getMinutes();
    minu = (minu<10 ? "0"+minu:minu);
    var s = t.getSeconds();
    s = (s<10 ? "0"+s:s);
    var str = y.toString()+"-"+m.toString()+"-"+d+"  "+h+":"+minu+":"+s;
    return str;
},
tick2DayStr:function(tick)
{
    var t=new Date();
    t.setTime(tick);
    var y = t.getFullYear();
    var m = t.getMonth()+1;
    m = (m<10 ? "0"+m:m);
    var d = t.getDate();
    d = (d<10 ? "0"+d:d);
    var str = y.toString()+"-"+m.toString()+"-"+d;
    return str;
},
    getCurWeek:function()
    {
        var d=new Date();
        d.setHours(0);d.setMinutes(0);d.setSeconds(0);d.setMilliseconds(0);
        var time= d.getTime();
        var day= d.getDay()-1;
        var  t=day*24*60*60*1000;
        d.setTime(time-t);
        var year= d.getFullYear();
        var mon= d.getMonth()+1;if(mon<10) mon='0'+mon;
        var date= d.getDate();if(date<10) date='0'+date;
        return year+'-'+mon+'-'+date;
    },
    getLastWeek:function(n)
    {
        var d=new Date();
        d.setHours(0);d.setMinutes(0);d.setSeconds(0);d.setMilliseconds(0);
        var time= d.getTime();
        var day= d.getDay()-1;
        if(day<0) day=6;
        var  t=(day+n*7)*24*60*60*1000;
        d.setTime(time-t);
        var year= d.getFullYear();
        var mon= d.getMonth()+1;if(mon<10) mon='0'+mon;
        var date= d.getDate();if(date<10) date='0'+date;
        return year+'-'+mon+'-'+date;
    },
    writeLog:function(dir,str)
    {
        var t=new Date();
        var year= t.getFullYear();
        var mon= t.getMonth()+1;if(mon<10) mon='0'+mon;
        var date= t.getDate();if(date<10) date='0'+date;
        var hour = t.getHours(); if(hour<10) hour='0'+hour;
        var minute= t.getMinutes(); if(minute<10) minute='0'+minute;
        var second= t.getSeconds(); if(second<10) second='0'+second;
        var  ms= Math.floor((t.getTime()%10000)/10);
        var m=Math.floor(minute/15);
        var file=dir+year+'-'+mon+'-'+date+' '+hour+'-'+m+'.txt';
        writeFile(file,str+'\r\n');
    },
};





module.exports=util;